package ch.ebnoether.wuerfel;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Dice extends Canvas {

  private static final long serialVersionUID = -2976129567610791919L;

  public Color augenfarbe;
  public Color wuerfelFarbeSpielbar;
  public Color wuerfelFarbeBlockiert;

  private int size;
  private int augenzahl;
  private int augenGroesse;
  private int augenPosition;
  private boolean wuerfelbar;

  public Dice(int size, Color augenfarbe, Color wuerfelFarbeSpielbar, Color wuerfelFarbeBlockiert) {
    this.augenfarbe = augenfarbe;
    this.wuerfelFarbeSpielbar = wuerfelFarbeSpielbar;
    this.wuerfelFarbeBlockiert = wuerfelFarbeBlockiert;
    this.size = size;

    setSize(size, size);
    setBackground(wuerfelFarbeSpielbar);
    this.augenGroesse = this.size / 6;
    this.wuerfelbar = true;
  }

  public void wuerfeln() {
    if (wuerfelbar) {
      this.augenzahl = random();
      repaint();
    }
  }

  public void zuruecksetzen() {
    this.setBackground(wuerfelFarbeSpielbar);
    this.wuerfelbar = true;
  }

  public void changeStatus() {
    if (wuerfelbar) {
      this.wuerfelbar = false;
      this.setBackground(wuerfelFarbeBlockiert);
    } else {
      this.wuerfelbar = true;
      this.setBackground(wuerfelFarbeSpielbar);
    }
  }

  @Override
  public void paint(Graphics g) {
    g.setColor(augenfarbe);

    switch (this.augenzahl) {
      case 1:

        this.augenPosition = size / 2 - (augenGroesse / 2);
        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);
        break;
      case 2:

        this.augenPosition = size / 4 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition + size / 2), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);
        break;
      case 3:

        this.augenPosition = size / 2 - (augenGroesse / 2);
        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        this.augenPosition = size / 4 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition + size / 2), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);
        break;
      case 4:

        this.augenPosition = size / 4 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval(this.augenPosition + (size / 2), this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition + size / 2), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);
        break;
      case 5:

        this.augenPosition = size / 2 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        this.augenPosition = size / 4 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval(this.augenPosition + (size / 2), this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition + size / 2), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);
        break;
      case 6:

        this.augenPosition = size / 2 - (augenGroesse / 2);

        g.fillOval(this.augenPosition + (size / 4), this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval(this.augenPosition - (size / 4), this.augenPosition, this.augenGroesse, this.augenGroesse);

        this.augenPosition = size / 4 - (augenGroesse / 2);

        g.fillOval(this.augenPosition, this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval(this.augenPosition + (size / 2), this.augenPosition, this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);

        g.fillOval((this.augenPosition + size / 2), (this.augenPosition + size / 2), this.augenGroesse, this.augenGroesse);
        break;
      default:
        break;
    }

  }

  private int random() {
    Random r = new Random();
    return r.nextInt(6) + 1;
  }

  public int getAugenzahl() {
    return augenzahl;
  }

  public void setAugenzahl(int augenzahl) {
    this.augenzahl = augenzahl;
  }

  public void setAugenfarbe(Color augenfarbe1) {
    augenfarbe = augenfarbe1;
  }

  public void setWuerfelFarbeSpielbar(Color wuerfelFarbeSpielbar1) {
    wuerfelFarbeSpielbar = wuerfelFarbeSpielbar1;
  }

  public void setWuerfelFarbeBlockiert(Color wuerfelFarbeBlockiert1) {
    wuerfelFarbeBlockiert = wuerfelFarbeBlockiert1;
  }
}
